﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Graphics;
using Tile_Engine;

namespace AGrandVenture
{
    public static class LevelManager
    {
        #region Declarations

        private static int currentLevel;
        private static Random random = new Random();

        public static List<Resource> Resources = new List<Resource>();

        #endregion

        #region Public Methods

        public static void Update(GameTime gameTime)
        {

        }

        public static void Draw(SpriteBatch spriteBatch)
        {
            foreach (Resource resource in Resources)
                resource.Draw(spriteBatch);
        }

        public static void LoadLevel(int levelNumber)
        {
            TileMap.LoadMap((System.IO.FileStream)TitleContainer.OpenStream(@"Content\Maps\MAP" + levelNumber.ToString().PadLeft(3, '0') + ".MAP"));
            DetermineStartLoc();
            PlaceEnemies();
            PlaceChests();
        }

        public static void DetermineStartLoc()
        {
            string codeToCheck;
            for (int x = 0; x < TileMap.MapWidth; x++)
            {
                for (int y = 0; y < TileMap.MapHeight; y++)
                {
                    codeToCheck = TileMap.CellCodeValue(x, y);

                    if (codeToCheck == "START")
                    {
                        Player.BaseSprite.WorldLocation = new Vector2((x * TileMap.TileWidth), (y * TileMap.TileHeight));
                        return;
                    }
                }
            }
        }

        public static void PlaceEnemies()
        {
            string codeToCheck;
            for (int x = 0; x < TileMap.MapWidth; x++)
            {
                for (int y = 0; y < TileMap.MapHeight; y++)
                {
                    codeToCheck = TileMap.CellCodeValue(x, y);

                    if (codeToCheck == "ENEMY")
                    {
                        GoalManager.AddGenerator(x, y);
                    }
                }
            }
        }

        public static void PlaceChests()
        {
            string codeToCheck;
            for (int x = 0; x < TileMap.MapWidth; x++)
            {
                for (int y = 0; y < TileMap.MapHeight; y++)
                {
                    codeToCheck = TileMap.CellCodeValue(x, y);

                    if (codeToCheck == "CHEST")
                    {
                        // add code for chests.  Right now just draws loot.  
                        LootItem lootItem = new LootItem(SeedUtility.newSeed(Player.Character.Level+ random.Next(0, 7)), new Vector2(x * TileMap.TileWidth, y * TileMap.TileHeight));
                        LootManager.addLoot(lootItem);
                    }
                }
            }
        }

        public static void GenerateRandomResources()
        {
            int resourceChancePerSquare = 2; // right now this will only be checked on wall tiles and will generate a mineral vein.  

            for (int x = 0; x < TileMap.MapWidth; x++)
            {
                for (int y = 0; y < TileMap.MapHeight; y++)
                {
                    if ((x == 0) || (y == 0) || (x == TileMap.MapWidth - 1) || (y == TileMap.MapHeight - 1))
                    {
                        //Generate nothing in walls on very edge of map
                        continue;
                    }

                    if ((x == 1) || (y == 1) || (x == TileMap.MapWidth - 2) || (y == TileMap.MapHeight - 2))
                    {
                        //generate nothing on floor on edge of map
                        continue;
                    }

                    MapSquare squareToCheck = TileMap.GetMapSquareAtCell(x,y); //get MapSquare at location x,y

                    if (!squareToCheck.Passable) // check if square is a wall
                    {
                        if (random.Next(0, 100) <= resourceChancePerSquare) //if square is wall, resourceChancePerSquare% chance it will hold a mineral vein
                        {
                            int mineraltype = random.Next(0, 100);

                            if ((mineraltype >=0) && (mineraltype < 50))
                                Resources.Add(new Resource("Copper", 10, x, y, Game1.resourceSheet, new Rectangle(0,0,48,48), Color.IndianRed));
 
                            if ((mineraltype >=50) && (mineraltype < 90))
                                Resources.Add(new Resource("Silver", 25, x, y, Game1.resourceSheet, new Rectangle(0, 0, 48, 48), Color.Silver));
                            
                            if (mineraltype > 90)
                                Resources.Add(new Resource("Gold", 50, x, y, Game1.resourceSheet, new Rectangle(0,0,48,48), Color.Gold));
                        }
                    }
                }
            }
        }

        #endregion
    }
}
